package com.hmall.gateway.routers;

import cn.hutool.json.JSONUtil;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executor;

@Component
@Slf4j
@RequiredArgsConstructor
public class DynamicRouteLoader {
    private  final NacosConfigManager nacosConfigManager;
    private  final RouteDefinitionWriter writer;
    private  final  String dataId="gateway-routes.json";
    private  final  String group="DEFAULT_GROUP";
    private final Set<String> routesId=new HashSet<>();
    @PostConstruct
    public  void initRouteConfigListener() throws NacosException {
        String  configInfo = nacosConfigManager.getConfigService().getConfigAndSignListener(dataId, group, 5000, new Listener() {
            @Override
            public Executor getExecutor() {
                return null;
            }

            @Override
            public void receiveConfigInfo(String configInfo) {
                     updateRouteConfig(configInfo);
            }
        });
        updateRouteConfig(configInfo);
    }
    public  void  updateRouteConfig(String configInfo){
        log.info("更新路由配置:{}",configInfo);
        log.info("{}",routesId.size());
        List<RouteDefinition> routeDefinitions= JSONUtil.toList(configInfo, RouteDefinition.class);
        for(String id:routesId){
            writer.delete(Mono.just(id)).subscribe();
        }
        routesId.clear();
        routeDefinitions.forEach(x->{
            writer.save(Mono.just(x)).subscribe();
            routesId.add(x.getId());
        });
    }
}
